package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysSchoolRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysSchoolMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysSchool;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysSchoolService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学校管理业务层处理
 *
 * @author mars
 * @date 2024-03-17
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysSchoolServiceImpl implements ISysSchoolService {

    private final SysSchoolMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysSchool add(SysSchoolRequest request) {
        SysSchool entity = SysSchool.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysSchoolRequest request) {
        SysSchool entity = SysSchool.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public SysSchool getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysSchool> pageList(SysSchoolRequest request) {
        Page<SysSchool> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysSchool> query = this.buildWrapper(request);
        IPage<SysSchool> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysSchool> list(SysSchoolRequest request) {
        LambdaQueryWrapper<SysSchool> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysSchool> buildWrapper(SysSchoolRequest param) {
        LambdaQueryWrapper<SysSchool> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getSchoolName())){
               query.like(SysSchool::getSchoolName ,param.getSchoolName());
        }
         if (StringUtils.isNotBlank(param.getSchoolAddress())){
               query.like(SysSchool::getSchoolAddress ,param.getSchoolAddress());
        }
         if (StringUtils.isNotBlank(param.getNumberSchool())){
               query.like(SysSchool::getNumberSchool ,param.getNumberSchool());
        }
         if (StringUtils.isNotBlank(param.getSchoolTeachers())){
               query.like(SysSchool::getSchoolTeachers ,param.getSchoolTeachers());
        }
         if (StringUtils.isNotBlank(param.getSchoolProfile())){
               query.like(SysSchool::getSchoolProfile ,param.getSchoolProfile());
        }
        return query;
    }

}
